﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApp.CommonSupport.Filter;
using DoNet.Common.Helper;
using DoNet.FrameWork.Repository.Specification;
using Authority.Entity;
using Authority.Services;
using MvcApp.Models;
using MvcApp.CommonSupport;
using System.Linq.Expressions;

namespace MvcApp.Controllers
{
    public class PermisionController : Controller
    {
        private readonly IModulesService _modulesService;
        private readonly IPermisionService _permisionService;

        public PermisionController(IModulesService modulesService, IPermisionService permisionService)
        {
            _modulesService = modulesService;
            _permisionService = permisionService;
        }

        public ActionResult Index(string postId, string userId)
        {
            string permisionJsonString = "[";
            List<UserControlsMap> ucms = new List<UserControlsMap>();
            if (userId != null && !string.IsNullOrEmpty(userId))
            {
               ucms = _permisionService.GetUserPermision(new Guid(userId));
                foreach (var ucm in ucms)
                {
                    permisionJsonString += "{";
                    permisionJsonString += "modules:'" + ucm.ModulesCode;
                    permisionJsonString += "',permisionTypes:'" + ucm.PermisionTypes;
                    permisionJsonString += "'},";
                }
            }
            
            if(ucms.Count == 0)
            {
                List<PostControlsMap> pcms = _permisionService.GetPostPermision(new Guid(postId));
                foreach (var pcm in pcms)
                {
                    permisionJsonString += "{";
                    permisionJsonString += "modules:'" + pcm.ModulesCode;
                    permisionJsonString += "',permisionTypes:'" + pcm.PermisionTypes;
                    permisionJsonString += "'},";
                }
            }
            permisionJsonString = permisionJsonString.Trim(',');

            permisionJsonString += "]";
            ViewBag.permisionJsonString = permisionJsonString;

            List<Modules> modules = _modulesService.GetALLModules();
            return View(modules);
        }

        [HttpPost]
        public ActionResult UpdatePermision(String postId, string userId, List<string> codes, List<string> permisionTypes)
        {
            CurrentUser currentUser = new CurrentUser();
            if (userId != null && !string.IsNullOrEmpty(userId))
            {
                Guid userGuid = new Guid(userId);
                List<UserControlsMap> ucms = new List<UserControlsMap>();
                if (codes != null)
                {
                    int length = codes.Count;
                    for (int i = 0; i < length; i++)
                    {
                        UserControlsMap ucm = new UserControlsMap();
                        ucm.UserId = userGuid;
                        ucm.ModulesCode = codes[i].Trim();
                        ucm.PermisionTypes = int.Parse(permisionTypes[i].Trim());
                        ucm.SysCreatedBy = currentUser.UserInfo.Id;
                        ucm.SysCreatedOn = DateTime.Now;
                        ucm.SysModifiedBy = currentUser.UserInfo.Id;
                        ucm.SysModifiedOn = DateTime.Now;
                        ucms.Add(ucm);
                    }
                }
                return Content(_permisionService.UpdateUserPermision(userGuid, ucms));
            }
            else
            {

                Guid postGuid = new Guid(postId);
                List<PostControlsMap> pcms = new List<PostControlsMap>();
                if (codes != null)
                {
                    int length = codes.Count;
                    for (int i = 0; i < length; i++)
                    {
                        PostControlsMap pcm = new PostControlsMap();
                        pcm.PostId = postGuid;
                        pcm.ModulesCode = codes[i].Trim();
                        pcm.PermisionTypes = int.Parse(permisionTypes[i].Trim());
                        pcm.SysCreatedBy = currentUser.UserInfo.Id;
                        pcm.SysCreatedOn = DateTime.Now;
                        pcm.SysModifiedBy = currentUser.UserInfo.Id;
                        pcm.SysModifiedOn = DateTime.Now;
                        pcms.Add(pcm);
                    }
                }
                return Content(_permisionService.UpdatePostPermision(postGuid, pcms));
            }
        }
    }
}
